<?php
/*
 * @Descripttion: 
 * @Author: dedeye
 * @Date: 2020-02-25 16:18:07
 * @LastEditors: dedeye
 * @LastEditTime: 2020-03-01 15:44:28
 */
namespace app\admin\controller;

use app\common\util\ReturnCode;
use app\common\util\Tool;
use app\admin\model\Rule;
use app\admin\model\Admin;
use think\facade\Session;

class Index extends Controller
{
    public function index()
    {
        return $this->fetch();
    }

    public function logout()
    {
        Session::delete('admin_id');
        Session::delete('admin_name');
        Session::delete('admin_code');
        return resultJson(1, '退出成功', url('admin/login/index'));
    }

    public function init()
    {
        $superAdmin = config('admin.super_admin');
        $admin_id = Session::get('admin_id');
        $init = config('admin.init');
        $init['menuInfo'] = [];
        $field = 'id, name as title, fid, url as href, target, hash, icon';
        if($superAdmin == $admin_id) {
            $menu = Rule::field($field)->where(['status'=> 1, 'is_menu' => 1])->select();
            if(!$menu->isEmpty())
                $init['menuInfo'] = Tool::getMenu($menu->toArray());
            else
                $init['menuInfo'] = config('admin.default_menu');
        }else {
            $adminRule = Admin::field('id')
                        ->with('groups')
                        ->get($admin_id);
            if(!is_null($adminRule)) {
                $adminRule = $adminRule->toArray();
                if(count($adminRule['groups']) > 0) {
                    $rules = '';
                    foreach($adminRule['groups'] as $k => $v) {
                        $rules .= $v['rules'] . ',';
                    }
                    $menu = Rule::field($field)
                                ->where([
                                    ['id', 'in', $rules],
                                    ['status', '=', 1],
                                    ['is_menu', '=', 1],
                                ])->all();
                    if(!$menu->isEmpty())
                        $init['menuInfo'] = Tool::getMenu($menu->toArray());
                }
            }
        }
        return resultJson(ReturnCode::SUCCESS, 'success', $init);
    }
    
    public function welcome()
    {
        return '<div style="margin:100px;font-size:48px;text-align:center;">欢迎来到DEDECMS6</div>';
    }

    public function noauth()
    {
        return '<div style="margin:100px;font-size:48px;text-align:center;">访问权限不足</div>';
    }
}
